package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
 * 会员服务
 * */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //根据手机号查询会员
    @Override
    public Member findByTelephone(String telephone) {
        Member member = memberDao.findByTelephone(telephone);
        //如果不是会员，就注册会员
        if (member == null) {
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            //注册会员
            memberDao.add(member);
        }
        return member;
    }

    //根据日期查询会员个数
    @Override
    public List<Integer> findMemberCountByDate(List<String> months) {
        List<Integer> list =new ArrayList<>();
        for (String month : months) {
            String date=month+".31";
            Integer count = memberDao.findMemberCountBeforeDate(date);
            list.add(count);
        }
        return list;
    }

    //新增会员(带密码)
    public void add(Member member) {
        if(member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }
}
